package com.exam.system.controller;

import com.exam.system.pojo.Permission;
import com.exam.system.service.IconService;
import com.exam.system.service.PermissionService;
import com.exam.system.util.AjaxResult;
import com.exam.system.util.Const;
import com.exam.system.util.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.HashMap;
import java.util.List;

/**
 * @Classname PermissionController
 * @Description None
 * @Date 2019/8/10 17:47
 * @Created by WDD
 */
@Controller
@RequestMapping("/manager")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private IconService iconService;

    /**
     * 跳转登录界面
     * @return
     */
    @GetMapping("/permission")
    public String permission(){
        return "manager/permission/permissionList";
    }

    /**
     * 异步加载权限树
     * @return
     */
    @GetMapping("/permissionList")
    @ResponseBody
    public Object permissionList(Integer id){
        List<Permission> permissionList = permissionService.selectAll();
        if(id != null){
            List<Permission> atList = permissionService.selectByRoleId(id);
            for(Permission info : permissionList){
                for(Permission at : atList){
                    if(info.getId().equals(at.getId())){
                        info.setChecked(true);
                    }
                }
            }
        }
        HashMap<String, Object> rest = new HashMap<>();
        rest.put("code",0);
        rest.put("msg","ok");
        rest.put("data",permissionList);

        return rest;
    }

    /**
     * 跳转页面
     * @param type
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/addPermission")
    public String addPermission(String type, Integer id, Model model, HttpSession session){
        //加载icon列表,存入session
        if(session.getAttribute(Const.ICON_LSIT) == null){
            List<Icon> iconList = iconService.selectAll();
            session.setAttribute(Const.ICON_LSIT,iconList);
        }
        if(type != null && type.equals("edit")){
            Permission permission = permissionService.selectById(id);
            model.addAttribute(Const.PERMISSION,permission);
        }else if(type != null && type.equals("add")){
            model.addAttribute("pid",id); //设为父id
        }else if(type != null && type.equals("addParent")){
            return "manager/permission/addParentNode";
        }
        return "manager/permission/addPermission";
    }

    /**
     * 添加修改权限
     * @param permission
     * @return
     */
    @PostMapping("/addPermission")
    @ResponseBody
    public AjaxResult submitAddPermission(Permission permission){
        Permission byName = permissionService.selectByName(permission.getName());
        Permission byUrl = null;
        if(!permission.getUrl().equals("-1")){
            byUrl = permissionService.selectByUrl(permission.getUrl());
        }

        if(permission.getId() !=null){
            //修改
            if(byName != null && !byName.getId().equals(permission.getId())){
                return AjaxResult.fail("权限名已存在");
            }
            if(byUrl != null && !byUrl.getId().equals(permission.getId())){
                return AjaxResult.fail("地址已存在");
            }
            int count = permissionService.editByPermission(permission);
            if(count > 0){
                return AjaxResult.success("修改成功");
            }
            return AjaxResult.fail("修改失败");
        }else{
            //添加
            if(byName != null){
                return AjaxResult.fail("权限名已存在");
            }
            if(byUrl != null){
                return AjaxResult.fail("地址已存在");
            }
            //若是添加父节点
            if(permission.getPid().equals(-1)){
                permission.setUrl("-1");
            }
            int count = permissionService.insertPermission(permission);
            if(count > 0){
                return AjaxResult.success("添加成功");
            }
            return AjaxResult.fail("添加失败");
        }
    }

    /**
     * 删除权限
     * @param data
     * @return
     */
    @PostMapping("/delPermission")
    @ResponseBody
    public AjaxResult delPermission(Data data){
        List<Permission> permissionList = permissionService.selectByPid(data.getIds().get(0));
        if(permissionList.size() !=0 ){
            return AjaxResult.fail("请先删除子节点");
        }
        int count = permissionService.delByPermissionIds(data.getIds());
        if(count >= data.getIds().size()){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.fail("删除失败");
    }

    /**
     * 给角色分配权限
     * @param data
     * @param id
     * @return
     */
    @PostMapping("/allotPer")
    @ResponseBody
    public AjaxResult allotPre(Data data,Integer id){
        int count = permissionService.updateRolePermission(data.getIds(),id);
        if(count >= data.getIds().size()){
            return AjaxResult.success("分配成功");
        }
        return null;
    }
}
